
const { ccclass, property } = cc._decorator;

export enum TweenType {
    无函数,
    平方曲线缓入函数,
    平方曲线缓出函数,
    平方曲线缓入缓出函数,
    立方曲线缓入函数,
    立方曲线缓出函数,
    立方曲线缓入缓出函数,
    四次方曲线缓入函数,
    四次方曲线缓出函数,
    四次方曲线缓入缓出函数,
    五次方曲线缓入函数,
    五次方曲线缓出函数,
    五次方曲线缓入缓出函数,
    正弦曲线缓入函数,
    正弦曲线缓出函数,
    正弦曲线缓入缓出函数,
    指数曲线缓入函数,
    指数曲线缓出函数,
    指数曲线缓入和缓出函数,
    循环公式缓入函数,
    循环公式缓出函数,
    指数曲线缓入缓出函数,
    弹簧回震效果的缓入函数,
    弹簧回震效果的缓出函数,
    弹簧回震效果的缓入缓出函数,
    回退效果的缓入函数,
    回退效果的缓出函数,
    回退效果的缓入缓出函数,
    弹跳效果的缓入函数,
    弹跳效果的缓出函数,
    弹跳效果的缓入缓出函数,
    平滑效果函数,
    渐褪效果函数,
}

@ccclass
export default class WTool extends cc.Component {
    public static TweenTypeName: string[] = ["", "quadIn", "quadOut", "quadInOut", "cubicIn", "cubicOut", "cubicInOut", "quartIn", "quartOut",
        "quartInOut", "quintIn", "quintOut", "quintInOut", "sineIn", "sineOut", "sineInOut", "expoIn", "expoOut", "expoInOut", "circIn", "circOut",
        "circInOut", "elasticIn", "elasticOut", "elasticInOut", "backIn", "backOut", "backInOut", "bounceIn", "bounceOut", "bounceInOut",
        "smooth", "fade",];
    //获取世界坐标
    public static setWorldPos(node: cc.Node, pos: cc.Vec2) {
        if (node.parent && pos != null) {
            node.setPosition(node.parent.convertToNodeSpaceAR(pos));//将点转换成局部坐标系
        }
    }

    /* 获取世界坐标，参数一为所需要获取的物体 */
    public static getWorldPos(node: cc.Node): cc.Vec2 {
        return node.convertToWorldSpaceAR(cc.Vec2.ZERO);
    }
    /* 获取两点之间的角度 */
    public static getAngle(startPos: cc.Vec2, endPos: cc.Vec2) {
        var dir = endPos.sub(startPos);
        return 180 / Math.PI * Math.atan2(dir.y, dir.x);
    }
    //获得两物体之间的距离
    public static getdistance(ns1: cc.Node, ns2: cc.Node): number {
        let ns1pos: cc.Vec2 = WTool.getWorldPos(ns1);
        let ns2pos: cc.Vec2 = WTool.getWorldPos(ns2);
        let Juli = ns1pos.sub(ns2pos).mag();
        return Juli;
    }
    //设置父物体位置不变
    public static SetParent(childrennode: cc.Node, Fathernode: cc.Node) {
        let Pos = childrennode.convertToWorldSpaceAR(cc.Vec2.ZERO);
        childrennode.setParent(Fathernode);
        childrennode.setPosition(childrennode.parent.convertToNodeSpaceAR(Pos));
    }
    //将物体的世界坐标设置为第二个参数物体的世界坐标
    public static ToWorldPos(node1: cc.Node, node2: cc.Node) {
        let pos = node2.convertToWorldSpaceAR(cc.Vec2.ZERO);
        node1.setPosition(node1.parent.convertToNodeSpaceAR(pos));
    }
    //获取物体世界缩放
    public static getWorldScale(node: cc.Node): number {
        let parentnode: cc.Node = node.parent;
        let Scales: number = node.scale;
        while (parentnode.name != "Canvas") {
            Scales *= parentnode.scale;
            parentnode = parentnode.parent;
        }
        return Scales;
    }
    //判断两个数组内容是否完全相等
    public static ArrayIsEquality(array1: any[], array2: any[]) {
        if (array1.length != array2.length) return false;
        for (let i = 0; i < array1.length; i++) {
            if (array1[i] != array2[i]) return false;
        }
        return true;
    }
    //-----------------------------------------缓动类------------------------------------------------------
    //缓动：将一个物体从a点移动到b点(世界坐标)
    public static Tween_Move(ns: cc.Node, a: cc.Vec2, b: cc.Vec2, _time: number = 1, _easing: string = "linear", callback = null) {
        if (a == null) {
            a = ns.convertToWorldSpaceAR(cc.Vec2.ZERO);
        }
        WTool.setWorldPos(ns, a);
        let Pianyi = b.sub(a);
        cc.tween(ns)
            .by(_time, { position: cc.v3(Pianyi.x, Pianyi.y) }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    //缓动：将一个物体到a点(世界坐标)
    public static Tween_To(ns: cc.Node, a: cc.Vec2, _time: number = 1, _easing: string = "linear", callback = null) {
        let Pianyi = a.sub(this.getWorldPos(ns));
        cc.tween(ns)
            .by(_time, { position: cc.v3(Pianyi.x, Pianyi.y) }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    //缓动，一个物体移动到a点(相对坐标)
    public static Tween_to(ns: cc.Node, a: cc.Vec2, _time: number = 1, _easing: string = "linear", callback = null) {
        cc.tween(ns)
            .to(_time, { position: cc.v3(a.x, a.y) }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    //缓动：将一个物体瞬移到场景外或者某个点，然后缓缓归位
    public static Tween_Outofareaaccess(ns: cc.Node, a: cc.Vec2, _time: number = 1, _easing: string = "linear", callback = null) {
        cc.tween(ns)
            .by(0, { position: cc.v3(a.x, a.y) })
            .call(() => { ns.active = true; })
            .by(_time, { position: cc.v3(-a.x, -a.y) }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    //缓动：将一个物体相对移动
    public static Tween_By(ns: cc.Node, a: cc.Vec2, _time: number = 1, _easing: string = "linear", callback = null) {
        cc.tween(ns)
            .by(_time, { position: cc.v3(a.x, a.y) }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    //缓动:物体透明度变化
    public static Tween_Opacity(ns: cc.Node, _time: number, initialOpacity: number = 0, OverOpacity: number = 255, callback = null) {
        ns.active = true;
        ns.opacity = initialOpacity;
        cc.tween(ns)
            .to(_time, { opacity: OverOpacity })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    //缓动:物体大小变化
    public static Tween_Scale(ns: cc.Node, _time: number, initiaScale: number = 0, OverScale: number = 1, callback = null, _easing: string = "linear") {
        ns.active = true;
        ns.scale = initiaScale;
        cc.tween(ns)
            .to(_time, { scale: OverScale }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    public static Tween_Angle(ns: cc.Node, _time: number, Angle: number = 0, callback = null, _easing: string = "linear") {
        ns.active = true;
        cc.tween(ns)
            .to(_time, { angle: Angle }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .start();
    }
    //缓动：上下循环缓动
    public static Tween_UpDown(ns: cc.Node, _time: number = 0.8, Upscale: number = 1.02, Downscale: number = 1) {
        cc.tween(ns)
            .to(_time, { scaleY: Upscale })
            .to(_time, { scaleY: Downscale })
            .union()
            .repeatForever()
            .start();
    }

    public static Tween_AngleForever(ns: cc.Node, _time: number, Angle: number = 0, callback = null, _easing: string = "linear"): cc.Tween {
        ns.active = true;
        let tween = cc.tween(ns)
            .to(_time, { angle: Angle }, { easing: _easing })
            .call(() => {
                if (callback) callback();
            })
            .union()
            .repeatForever()
            .start();
        return tween;
    }




    //-----------------------------------------缓动类尾部------------------------------------------------------

}
